package com.xc.hotelmgr.datahandlermt.service.mt.impl;


import com.xc.hotelmgr.datahandlermt.pojo.mq.DelRpMQ;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.DtStore;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.HtLogicRoom;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.OpHtlGroup;
import com.xc.hotelmgr.datahandlermt.pojo.mybatis.OpTaskStatus;
import com.xc.hotelmgr.datahandlermt.service.mt.RpService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.DtStoreService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.OpHtlGroupService;
import com.xc.hotelmgr.datahandlermt.service.mybatis.OpTaskStatusService;
import com.xc.hotelmgr.datahandlermt.utils.MqType;
import com.xc.hotelmgr.datahandlermt.utils.SendMessageMgr;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RpServiceImpl implements RpService {

    private final OpHtlGroupService opHtlGroupService;

    private final SendMessageMgr sendMessageMgr;

    private final DtStoreService dtStoreService;

    private final static String STORE_HOTEL_TASK_CODE = "STOREHOTEL";

    private final static String STORE_HOTEL_STATE_NAME = "房型删除";

    private final OpTaskStatusService opTaskStatusService;


    @Override
    public boolean delRp(Integer storeId, Integer groupId, Integer opOurRateplanId) {
        List<OpHtlGroup> opHtlGroups = opHtlGroupService.getOpHtlGroupHtLogicRoomByGIdAndSId(groupId, storeId);
        if(opHtlGroups!=null) {
            // 执行酒店总数量
            int num = opHtlGroups.size();

            DtStore dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();

            // 生成新的任务
            OpTaskStatus opTaskStatus = new OpTaskStatus();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0);
            //总数
            opTaskStatus.setCompAmount(num);

            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatusService.insertIgnoreNull(opTaskStatus);

            Long taskId = opTaskStatus.getId();  // 任务id

            for (OpHtlGroup opHtlGroup:opHtlGroups) {
                List<HtLogicRoom> htLogicRooms = opHtlGroup.getHtLogicRooms();
                for (HtLogicRoom htLogicRoom:htLogicRooms) {
                    DelRpMQ delRpMQ = new DelRpMQ();
                    Long rpId = htLogicRoom.getId();
                    delRpMQ.setRpId(rpId);
                    delRpMQ.setTaskId(taskId);
                    delRpMQ.setOpOurRateplanId(opOurRateplanId);
                    sendMessageMgr.sendMessage(MqType.DELRP, delRpMQ, appKey);
                }
            }
            DelRpMQ delRpMQ = new DelRpMQ();
            delRpMQ.setRpId((long) -1);
            sendMessageMgr.sendMessage(MqType.DELRP, delRpMQ, appKey);
            return true;
        }
        return false;
    }
}
